import { AppRouteModule, AppRouteRecordRaw } from '@/router/types';

/**
 * 路由设置
 */
class RouterSetup {
  protected static SINGLE_INSTANCE: RouterSetup;
  private readonly _asyncRoutes: AppRouteRecordRaw[] = [];

  /**
   * 构造函数
   * @private
   */
  private constructor() {
    const modules = import.meta.glob('./modules/**/*.ts', { eager: true });
    const routeModuleList: AppRouteModule[] = [];
    Object.keys(modules).forEach((key) => {
      const mod = (modules as Recordable)[key].default || {};
      const modList = Array.isArray(mod) ? [...mod] : [mod];
      routeModuleList.push(...modList);
    });
    this._asyncRoutes = [...routeModuleList];
  }
  public static INSTANCE(): RouterSetup {
    if (RouterSetup.SINGLE_INSTANCE == null) {
      RouterSetup.SINGLE_INSTANCE = new RouterSetup();
    }
    return RouterSetup.SINGLE_INSTANCE;
  }
  get asyncRoutes(): AppRouteRecordRaw[] {
    return this._asyncRoutes;
  }
}
export const MY_ASYNC_ROUTES = RouterSetup.INSTANCE().asyncRoutes;
